//
//  AllCountryViewController.m
//  B
//
//  Created by Sunpeng on 15/11/9.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "AllCountryViewController.h"
#import "AllCountryCollectionViewCell.h"
#import "HTTPTool.h"
#import "SearchDetailViewController.h"
#import "BaseView.h"
@interface AllCountryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)NSMutableArray *allArray;
@end

@implementation AllCountryViewController
//-(void)viewWillAppear:(BOOL)animated {
//    
//    [super viewWillAppear:animated];
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = YES;
//        }
//    }
//}
//
//- (void) viewWillDisappear:(BOOL)animated {
//    
//    [super viewWillDisappear:animated];
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = NO;
//            
//        }
//    }
//    
//    
//}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
     
        self.hidesBottomBarWhenPushed = YES;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 10, WIDTH / 2 - 10);
    //距离上左下右  逆时针
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 5, 5);
    
    NSDictionary *dict=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-64) collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:251/255.0 green:247 /255.0 blue:236/255.0 alpha:1];
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[AllCountryCollectionViewCell class] forCellWithReuseIdentifier:@"AllCountry"];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.allArray = [NSMutableArray array];
    [self afnGetRequest];
}

-(void)afnGetRequest{
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%ld/",self.url] body:nil httpResult:JSON success:^(id result) {
        NSLog(@"====http://api.breadtrip.com/destination/index_places/%ld/",self.url);
        NSDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            AllCountryModel *countryM = [[AllCountryModel alloc]init];
            [countryM setValuesForKeysWithDictionary:dic];
            countryM.idString = [dic objectForKey:@"id"];
            
            [self.allArray addObject:countryM];
        }
        [self.collectionView reloadData];
        
        
        
        
        
    }
                failure:^(NSError *error) {
             
                     
                     
                     
                     
              //NSLog(@"%@",error);
          }];
    

    
    

    
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllCountryCollectionViewCell *countryCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AllCountry" forIndexPath:indexPath];
    AllCountryModel *countryM =[self.allArray objectAtIndex:indexPath.row];
    countryCell.countryM = countryM;
    return countryCell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllCountryModel *countryM = [self.allArray objectAtIndex:indexPath.row];
    SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
    detailVC.url = countryM.cover;
    detailVC.idString = countryM.idString;
    detailVC.typeNUmber = countryM.type;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
